package com.longzz.util;

import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by longzzz on 2018/6/27.
 */
@Component
@Log4j
public class FileUtil {

    /**
     * 处理下载文件名乱码
     *
     * @param fileName 文件名称
     * @return 处理后的文件名
     */
    public String encodingFileName(String fileName) {
        String returnFileName = "";
        try {
            returnFileName = URLEncoder.encode(fileName, "UTF-8");
            returnFileName = StringUtils.replace(returnFileName, "+", "%20");
            if (returnFileName.length() > 150) {
                returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
                returnFileName = StringUtils.replace(returnFileName, " ", "%20");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.info("Don't support this encoding ...");
        }
        return returnFileName;
    }

}
